import { defineStore } from "pinia";

const _local_name = 't2i-image'

interface ImageState {
    data: Image.Image[]
}

export const useImageStore = defineStore('image', {
    state: ():ImageState => {
        const res = localStorage.getItem(_local_name)
        if (res) {
            return JSON.parse(res)
        }
        return  {data: []}
    },
    getters: {
        getList: (state) => {
            return (start: number, end: number) => {
                console.log(state.data)
                return state.data.slice(start, end)
                // return []
            }
        },

    },
    actions: {
        add(item: Image.Image) {
            this.data.unshift(item)
            this.recordState()
        },
        remove(id: string) {
            const index = this.data.findIndex(item => item.id == id)
            if (index < 0) return
            this.data.splice(index, 1)
            this.recordState()
        },
        recordState() {
            localStorage.setItem(_local_name, JSON.stringify(this.$state))
        }
    }
})